<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CAD_Doc: Quickstart: Building with Bazel</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  $(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">CAD_Doc<span id="projectnumber">&#160;1.0</span>
   </div>
   <div id="projectbrief">cad synopsis</div>
  </td>
    <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <span id="MSearchSelect"                onmouseover="return searchBox.OnSearchSelectShow()"                onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
          <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('md__build_2__deps_2googletest-src_2docs_2quickstart-bazel.html',''); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">Quickstart: Building with Bazel</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md233"></a></p>
<p>This tutorial aims to get you up and running with GoogleTest using the Bazel build system. If you're using GoogleTest for the first time or need a refresher, we recommend this tutorial as a starting point.</p>
<h1><a class="anchor" id="autotoc_md234"></a>
Prerequisites</h1>
<p>To complete this tutorial, you'll need:</p>
<ul>
<li>A compatible operating system (e.g. Linux, macOS, Windows).</li>
<li>A compatible C++ compiler that supports at least C++14.</li>
<li><a href="https://bazel.build/">Bazel</a>, the preferred build system used by the GoogleTest team.</li>
</ul>
<p>See <a class="el" href="md__build_2__deps_2googletest-src_2docs_2platforms.html">Supported Platforms</a> for more information about platforms compatible with GoogleTest.</p>
<p>If you don't already have Bazel installed, see the <a href="https://bazel.build/install">Bazel installation guide</a>.</p>
<p>{: .callout .note} Note: The terminal commands in this tutorial show a Unix shell prompt, but the commands work on the Windows command line as well.</p>
<h1><a class="anchor" id="autotoc_md235"></a>
Set up a Bazel workspace</h1>
<p>A <a href="https://docs.bazel.build/versions/main/build-ref.html#workspace">Bazel workspace</a> is a directory on your filesystem that you use to manage source files for the software you want to build. Each workspace directory has a text file named <code>WORKSPACE</code> which may be empty, or may contain references to external dependencies required to build the outputs.</p>
<p>First, create a directory for your workspace:</p>
<div class="fragment"><div class="line">$ mkdir my_workspace &amp;&amp; cd my_workspace</div>
</div><!-- fragment --><p>Next, you’ll create the <code>WORKSPACE</code> file to specify dependencies. A common and recommended way to depend on GoogleTest is to use a <a href="https://docs.bazel.build/versions/main/external.html">Bazel external dependency</a> via the <a href="https://docs.bazel.build/versions/main/repo/http.html#http_archive"><code>http_archive</code> rule</a>. To do this, in the root directory of your workspace (<code>my_workspace/</code>), create a file named <code>WORKSPACE</code> with the following contents:</p>
<div class="fragment"><div class="line">load(&quot;@bazel_tools//tools/build_defs/repo:http.bzl&quot;, &quot;http_archive&quot;)</div>
<div class="line"> </div>
<div class="line">http_archive(</div>
<div class="line">  name = &quot;com_google_googletest&quot;,</div>
<div class="line">  urls = [&quot;https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip&quot;],</div>
<div class="line">  strip_prefix = &quot;googletest-609281088cfefc76f9d0ce82e1ff6c30cc3591e5&quot;,</div>
<div class="line">)</div>
</div><!-- fragment --><p>The above configuration declares a dependency on GoogleTest which is downloaded as a ZIP archive from GitHub. In the above example, <code>609281088cfefc76f9d0ce82e1ff6c30cc3591e5</code> is the Git commit hash of the GoogleTest version to use; we recommend updating the hash often to point to the latest version.</p>
<p>Now you're ready to build C++ code that uses GoogleTest.</p>
<h1><a class="anchor" id="autotoc_md236"></a>
Create and run a binary</h1>
<p>With your Bazel workspace set up, you can now use GoogleTest code within your own project.</p>
<p>As an example, create a file named <code>hello_test.cc</code> in your <code>my_workspace</code> directory with the following contents:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;gtest/gtest.h&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Demonstrate some basic assertions.</span></div>
<div class="line">TEST(HelloTest, BasicAssertions) {</div>
<div class="line">  <span class="comment">// Expect two strings not to be equal.</span></div>
<div class="line">  EXPECT_STRNE(<span class="stringliteral">&quot;hello&quot;</span>, <span class="stringliteral">&quot;world&quot;</span>);</div>
<div class="line">  <span class="comment">// Expect equality.</span></div>
<div class="line">  EXPECT_EQ(7 * 6, 42);</div>
<div class="line">}</div>
</div><!-- fragment --><p>GoogleTest provides <a href="primer.md#assertions">assertions</a> that you use to test the behavior of your code. The above sample includes the main GoogleTest header file and demonstrates some basic assertions.</p>
<p>To build the code, create a file named <code>BUILD</code> in the same directory with the following contents:</p>
<div class="fragment"><div class="line">cc_test(</div>
<div class="line">  name = &quot;hello_test&quot;,</div>
<div class="line">  size = &quot;small&quot;,</div>
<div class="line">  srcs = [&quot;hello_test.cc&quot;],</div>
<div class="line">  deps = [&quot;@com_google_googletest//:gtest_main&quot;],</div>
<div class="line">)</div>
</div><!-- fragment --><p>This <code>cc_test</code> rule declares the C++ test binary you want to build, and links to GoogleTest (<code>//:gtest_main</code>) using the prefix you specified in the <code>WORKSPACE</code> file (<code>@com_google_googletest</code>). For more information about Bazel <code>BUILD</code> files, see the <a href="https://docs.bazel.build/versions/main/tutorial/cpp.html">Bazel C++ Tutorial</a>.</p>
<p>Now you can build and run your test:</p>
<pre>
<b>my_workspace$ bazel test --test_output=all //:hello_test</b>
INFO: Analyzed target //:hello_test (26 packages loaded, 362 targets configured).
INFO: Found 1 test target...
INFO: From Testing //:hello_test:
==================== Test output for //:hello_test:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from HelloTest
[ RUN      ] HelloTest.BasicAssertions
[       OK ] HelloTest.BasicAssertions (0 ms)
[----------] 1 test from HelloTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.
================================================================================
Target //:hello_test up-to-date:
  bazel-bin/hello_test
INFO: Elapsed time: 4.190s, Critical Path: 3.05s
INFO: 27 processes: 8 internal, 19 linux-sandbox.
INFO: Build completed successfully, 27 total actions
//:hello_test                                                     PASSED in 0.1s

INFO: Build completed successfully, 27 total actions
</pre><p>Congratulations! You've successfully built and run a test binary using GoogleTest.</p>
<h1><a class="anchor" id="autotoc_md237"></a>
Next steps</h1>
<ul>
<li><a class="el" href="md__build_2__deps_2googletest-src_2docs_2primer.html">Check out the Primer</a> to start learning how to write simple tests.</li>
<li><a class="el" href="md__build_2__deps_2googletest-src_2docs_2samples.html">See the code samples</a> for more examples showing how to use a variety of GoogleTest features. </li>
</ul>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
  </ul>
</div>
</body>
</html>
